/*
 * MPR121.c
 *
 *  Created on: Mar 8, 2014
 *      Author: Misael Munoz
 */

#define MPR_Interface_C
/***************************************************************************
* HEADER-FILES (Only those that are needed in this file)
****************************************************************************/
/* System-headerfiles */
#include "evhdl_iterface.h"

/* Foreign headerfiles */
#include "i2c_interface.h"
#include "Sw_Timers.h"

/* Own headerfiles */
#include "MPR121_cfg.h"
#include "MPR121.h"


/***************************************************************************
* GLOBAL DEFINITIONS
****************************************************************************/

MPR_CInterface MPR__tpdData;

/**
* Values of the past lecture of the MPR12 Registers 
* @range  
* @values 0x00 ... 0xFF
*/
uint16 MPR_u16ElectrodeStatus = 0;



/***************************************************************************
* FILE LOCAL DEFINITIONS
****************************************************************************/
static uint8 MPR__su8aMPRInitConfig[] ={
      /*First write */
      0x01, //MHD Rising
      0x01, //NHD Amount Rising
      0x00, //NCL Rising
      0x00, //FDL Rising
      0x01, //MHD Falling
      0x01, //NHD Amount Falling
      0xFF, //NCL Falling
      0x02, //FDL Falling
      
      /*Second write */
      0x0F, //ELE0 Touch Threshold
      0x0A, //ELE0 Release Threshold
      0x0F, //ELE1 Touch Threshold
      0x0A, //ELE1 Release Threshold
      0x0F, //ELE2 Touch Threshold
      0x0A, //ELE2 Release Threshold
      0x0F, //ELE3 Touch Threshold
      0x0A, //ELE3 Release Threshold
      0x0F, //ELE4 Touch Threshold
      0x0A, //ELE4 Release Threshold
      0x0F, //ELE5 Touch Threshold
      0x0A, //ELE5 Release Threshold
      0x0F, //ELE6 Touch Threshold
      0x0A, //ELE6 Release Threshold
      0x0F, //ELE7 Touch Threshold
      0x0A, //ELE7 Release Threshold
      0x0F, //ELE8 Touch Threshold
      0x0A, //ELE8 Release Threshold
      0x0F, //ELE9 Touch Threshold
      0x0A, //ELE9 Release Threshold
      0x0F, //ELE10 Touch Threshold
      0x0A, //ELE10 Release Threshold
      0x0F, //ELE11 Touch Threshold
      0x0A, //ELE11 Release Threshold
     
      /*Third write */
      0x10, //AFE Configuration     
      0x04, //Filter Configuration
      0x0C, //Electrode Configuration
      
      /*Fourth write*/
      0x0B, //AUTO-CONFIG Control Register 0
      0x00, //AUTO-CONFIG Control Register 1
      0xC9, //AUTO-CONFIG USL Register
      0x83, //AUTO-CONFIG LSL Register
      0xB5, //AUTO-CONFIG Target Level Register
      
      /*Soft reset value*/
      0x63, // Soft Reset Value 
};

/******************************************************************************
* Function Definitions Local Scope  
******************************************************************************/
 

/******************************************************************************
* Function Definitions System Wide Scope 
******************************************************************************/
void MPR_HWInit(void)
{
#if (MPR121_DEBUG == True)
   int x = 0;
   for (x=0; x < MPR__nMaxBuffReg; x++)
   {
      MPR__u8aCurrentReg[x] = 0x01;
      MPR__u8aPastReg[x] = 0x02;
   }
#elif (MPR121_DEBUG == False)

#else
#error "Unknown configuration variable"
#endif     

   /*Soft reset to the device in order to erase previous configurations */
   MPR__vSoftReset(); 
   
   I2C_ReadJob(I2C_enTouchDev, ELE07TS, MPR__nAllMPRmemory, &MPR__u8aPastReg);
   MPR__WaitFinisJob;

   /*First write */
   I2C_WriteJob(I2C_enTouchDev, MHD_RIS, &MPR__su8aMPRInitConfig[N_MHD_RIS], MPR__nInitHWFirsWrite);
   MPR__WaitFinisJob;

   /*Second write */
   I2C_WriteJob(I2C_enTouchDev, ELE0_TT, &MPR__su8aMPRInitConfig[N_ELE0_TT], MPR__nInitHWSecoWrite);
   MPR__WaitFinisJob;

   /*Third write */
   I2C_WriteJob(I2C_enTouchDev, AFE_CONFIG, &MPR__su8aMPRInitConfig[N_AFE_CONFIG], MPR__nInitHWThirdWrite);
   MPR__WaitFinisJob;

   /*Fourth write */
   I2C_WriteJob(I2C_enTouchDev, AUTO_CR0, &MPR__su8aMPRInitConfig[N_AUTO_CR0], MPR__nInitHWFourthWrite);
   MPR__WaitFinisJob;

   MPR_vWriteElectrodeConfig();
   
   I2C_ReadJob(I2C_enTouchDev, ELE07TS, MPR__nAllMPRmemory, &MPR__u8aCurrentReg);
   MPR__WaitFinisJob;
  
#if (MPR121_MODE == MPR121_POLLING_MODE)
   /* Start timer to polling the reading of the electrodes */
   MPR__tpdData.u8SwTimerTouchPolling = SwTimer_CreateTimer(&MPR_vPollingELERead);
   SwTimer_StartTimer(MPR__tpdData.u8SwTimerTouchPolling, MPR__nPollingTime);

#elif (MPR121_MODE == MPR121_INTERRUPT_MODE)

#else
#error "Unknown configuration variable"
#endif    
   
}


void MPR__vFinishjobHandler(uint8 job)
{
   uint16 u16tempElectrode = 0;
   switch (job)
   {
   case I2C_nTXEvent:
      break;
   case I2C_nRXEvent:
      u16tempElectrode = MPR_u16ElectrodeStatus;      
      MPR_u16ElectrodeStatus = MPR__u8aCurrentReg[0];
      MPR_u16ElectrodeStatus |= (uint16)(MPR__u8aCurrentReg[1] << 8);
      
      if(u16tempElectrode != MPR_u16ElectrodeStatus)
      {
         /*Send event to GDD module */
         EVHD_vSendEvent(EV_GDD_NewData_Event);
      }
      
      
      break;
   default:
      break;
   }
}


#if (MPR121_MODE == MPR121_POLLING_MODE)

void MPR_vPollingELERead(void)
{
   I2C_ReadJob(I2C_enTouchDev, ELE07TS, 0x02, &MPR__u8aCurrentReg);
   SwTimer_StartTimer(MPR__tpdData.u8SwTimerTouchPolling, MPR__nPollingTime);
}

#elif (MPR121_MODE == MPR121_INTERRUPT_MODE)

void MPR_vInterruptELERead(void)
{
   I2C_ReadJob(I2C_enTouchDev, ELE07TS, 0x02, &MPR__u8aCurrentReg);
}

#else
#error "Unknown configuration variable"
#endif 




